package com.bilibili.lzx.service;

import com.alibaba.fastjson2.JSONObject;
import com.bilibili.lzx.dao.UserDao;
import com.bilibili.lzx.dao.domain.User;
import com.bilibili.lzx.dao.domain.UserInfo;
import com.bilibili.lzx.dao.domain.constant.UserConstant;
import com.bilibili.lzx.dao.domain.exception.ConditionException;
import com.bilibili.lzx.service.config.RocketMQConfig;
import com.bilibili.lzx.service.util.MD5Util;
import com.bilibili.lzx.service.util.RSAUtil;
import com.bilibili.lzx.service.util.TokenUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class UserService {
    @Resource
    private UserDao userDao;


    public int addUser(User user){
        String phone = user.getPhone();
        if(StringUtils.isNullOrEmpty(phone)){
            throw new ConditionException("手机号为空！");
        }
        User dbUser = getUserByPhone(phone);
        if (dbUser != null) {
            throw new ConditionException("用户已经存在！");
        }
        Date now = new Date();
        String salt = String.valueOf(now.getTime());
        String password = user.getPassword();
        String rawPassword;
        try {
            rawPassword = RSAUtil.decrypt(password);
        } catch (Exception e) {
            throw new ConditionException("密码解密失败");
        }
        String md5Password = MD5Util.sign(rawPassword, salt, "UTF-8");
        user.setPassword(md5Password);
        user.setSalt(salt);
        user.setCreateTime(now);
        userDao.addUser(user);
        //添加用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setNick(UserConstant.DEFAULT_NICK);
        userInfo.setGender(UserConstant.GENDER_MALE);
        userInfo.setBirth(UserConstant.DEFAULT_BIRTH);
        userInfo.setCreateTime(now);
        return userDao.addUserInfo(userInfo);
    }

    public User getUserByPhone(String phone) {
        return userDao.getUserByPhone(phone);
    }

    public String login(User user) {
        String phone = user.getPhone();
        if(StringUtils.isNullOrEmpty(phone)){
           throw new ConditionException("手机号不正确");
        }
        User dbUser = getUserByPhone(phone);
        if(dbUser==null){
            throw new ConditionException("用户不存在");
        }
        String password = user.getPassword();
        String rowPassword;
        try {
            rowPassword = RSAUtil.decrypt(password);
        } catch (Exception e) {
            throw new ConditionException("解密失败");
        }
        String salt = dbUser.getSalt();
        String md5Password = MD5Util.sign(rowPassword, salt, "UTF-8");
        if(!md5Password.equals(dbUser.getPassword())){
            throw new ConditionException("密码不正确");
        }
        String token;
        try {
            token = TokenUtil.generateToken(dbUser.getId());
        } catch (Exception e) {
            throw new ConditionException("获取失败");
        }
        return token;
    }

    public User getUserInfo(Long userId) {
        User user = userDao.getUserById(userId);
        UserInfo userInfo = userDao.getUserInfoByUserId(userId);
        user.setUserInfo(userInfo);
        return user;
    }

    public User getById(Long userId) {
        return userDao.getUserById(userId);
    }

    public List<UserInfo> getUserInfoByUserIds(Set<Long> userIds) {
        return userDao.getUserInfoByUserIds(userIds);
    }

    public PageInfo<UserInfo> pageListUserInfos(JSONObject params) {
        PageHelper.startPage(params.getIntValue("no"), params.getIntValue("size"));
        List<UserInfo> userInfoList = userDao.pageCountUserInfos(params.getString("nick"));
        return new PageInfo<>(userInfoList);
    }
}
